package com.glodon.jcloud.increment.shorturl.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.glodon.jcloud.common.api.APIResponse;
import com.glodon.jcloud.increment.api.BaseAPI;
import com.glodon.jcloud.increment.common.pojo.CommonValidList;
import com.glodon.jcloud.increment.shorturl.entities.ShortUrl;
import com.glodon.jcloud.increment.shorturl.param.*;
import com.glodon.jcloud.increment.shorturl.service.ShortUrlService;
import com.glodon.jcloud.starter.log.annotation.AsyncLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

/**
 * <p>
 * Create Date: 2022/12/12
 *
 * @version: V0.0.1 -SNAPSHOT
 * @author: Terry.Li
 * @mail: lib-o@glodon.com
 */
@Slf4j
@Api(tags = "字典API")
@ApiSupport(author = "TERRY_TEAM", order = 1)
@RestController
@RequestMapping
@Validated
public class ShortUrlApi extends BaseAPI {

    @Resource
    private ShortUrlService shortUrlService;

    /**
     * 获取短链接分页.
     *
     * @param shortUrlPageParam the shortUrl page param
     * @return the api response
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取短链接分页")
    @GetMapping("/management/page")
    public APIResponse<Page<ShortUrl>> page(ShortUrlPageParam shortUrlPageParam) {
        return success(shortUrlService.page(shortUrlPageParam));
    }

    /**
     * 添加短链接.
     *
     * @param shortUrlAddParam the shortUrl add param
     * @return the api response
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("添加短链接")
    @AsyncLog("添加短链接")
    @PostMapping("/management/add")
    public APIResponse<ShortUrl> add(@RequestBody @Valid ShortUrlAddParam shortUrlAddParam) {
        return success(shortUrlService.add(shortUrlAddParam));
    }

    /**
     * 编辑短链接.
     *
     * @param shortUrlEditParam the shortUrl edit param
     * @return the api response
     */
    @ApiOperationSupport(order = 3)
    @ApiOperation("编辑短链接")
    @AsyncLog("编辑短链接")
    @PostMapping("/management/edit")
    public APIResponse<String> edit(@RequestBody @Valid ShortUrlEditParam shortUrlEditParam) {
        shortUrlService.edit(shortUrlEditParam);
        return success();
    }

    /**
     * 删除短链接.
     *
     * @param shortUrlIdParamList the shortUrl id param list
     * @return the api response
     */
    @ApiOperationSupport(order = 4)
    @ApiOperation("删除短链接")
    @AsyncLog("删除短链接")
    @PostMapping("/management/delete")
    public APIResponse<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                      CommonValidList<ShortUrlIdParam> shortUrlIdParamList) {
        shortUrlService.delete(shortUrlIdParamList);
        return success();
    }

    /**
     * 获取短链接详情.
     *
     * @param shortUrlIdParam the shortUrl id param
     * @return the api response
     */
    @ApiOperationSupport(order = 5)
    @ApiOperation("获取短链接详情")
    @GetMapping("/management/detail")
    public APIResponse<ShortUrl> detail(@Valid ShortUrlIdParam shortUrlIdParam) {
        return success(shortUrlService.detail(shortUrlIdParam));
    }

    /**
     * 转换文本中短链接.
     *
     * @param shortUrlConvertParam the short url convert param
     * @return the api response
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation("转换文本中短链接")
    @AsyncLog("转换文本中短链接")
    @PostMapping("/management/convert")
    public APIResponse<String> convert(@RequestBody @Valid ShortUrlConvertParam shortUrlConvertParam) {
        return success(shortUrlService.convert(shortUrlConvertParam));
    }
}
